package net.qunzhi.modules.app.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qunzhi.common.utils.PageUtils;
import net.qunzhi.common.utils.R;
import net.qunzhi.modules.app.annotation.Login;
import net.qunzhi.modules.app.query.CopyDataQuery;
import net.qunzhi.modules.app.utils.JwtUtils;
import net.qunzhi.modules.cms.entity.TbCopyFlowDataEntity;
import net.qunzhi.modules.cms.service.TbCopyFlowDataService;
import net.qunzhi.modules.cms.service.TbUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;


/**
 * 手抄的流量计数据
 *
 * @author zjr
 * @date 2021-09-10 15:48:00
 */
@Api(tags = "App手抄的流量计数据相关")
@RestController
@RequestMapping("app/tbCopyFlowData")
public class AppCopyFlowDataController {
    @Autowired
    private TbCopyFlowDataService tbCopyFlowDataService;
    @Autowired
    private TbUserInfoService tbUserInfoService;
    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 列表
     */
    @Login
    @ApiOperation("列表")
    @GetMapping("/list")
    public R list(CopyDataQuery query){
        PageUtils page = tbCopyFlowDataService.queryPage(query);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @Login
    @ApiOperation("信息")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		TbCopyFlowDataEntity tbCopyFlowData = tbCopyFlowDataService.getById(id);

        return R.ok().put("tbCopyFlowData", tbCopyFlowData);
    }

    /**
     * 保存
     */
    @Login
    @ApiOperation("保存")
    @PostMapping("/save")
    public R save(@RequestBody TbCopyFlowDataEntity tbCopyFlowData, HttpServletRequest request){
        long userId = Long.parseLong(jwtUtils.getClaimByToken(request.getHeader(jwtUtils.getHeader())).getSubject());
        tbCopyFlowData.setCreateUserId(userId).setCreateUserName(tbUserInfoService.getById(userId).getUsername());
        tbCopyFlowDataService.save(tbCopyFlowData);

        return R.ok();
    }

    /**
     * 修改
     */
    @Login
    @ApiOperation("修改")
    @PostMapping("/update")
    public R update(@RequestBody TbCopyFlowDataEntity tbCopyFlowData){
		tbCopyFlowDataService.updateById(tbCopyFlowData);

        return R.ok();
    }

    /**
     * 删除
     */
    @Login
    @ApiOperation("删除")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		tbCopyFlowDataService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
